package io.gitee.hchaojie.day07;

import org.junit.Test;

import java.io.*;
import java.util.Date;

public class TestStream {
    // 使用字节流读文件
    @Test
    public void testByteStream() throws IOException {
        // FileNotFoundException
        InputStream is = new FileInputStream("data\\sentence.txt");
        
        // IOException
        int letter = is.read();
        
        System.out.println((char) letter);

        // 一次读多个字节

        byte[] bytes = new byte[10];
        int count = is.read(bytes);     // 读10个字节到bytes容器
        System.out.println(new String(bytes, 0, count));      // 根据字节数组，构建字符串

        // 流是连续的

        // 使用循环把流里面的数据读完
        while (true) {
            int c = is.read();

            if (c == -1) {
                break;
            }

            System.out.print((char) c);
        }

        int i = 0;
        while ((i = is.read()) != -1) {
            System.out.print((char) i);
        }
    }

    @Test
    public void test() {
        try (InputStream is = new FileInputStream("data\\sentence.txt")) {
            byte[] bytes = new byte[10];

            while (true) {
                int count = is.read(bytes);

                if (count == -1) {
                    break;
                }

                System.out.println(new String(bytes, 0, count));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // IO资源要释放

    @Test
    public void testCloseStream() {
        // {} 里面是一个新的作用域：块级作用域
        InputStream is = null;
        try {
            System.out.println("1######");
            is = new FileInputStream("data\\sentence1.txt");
            System.out.println("2######");

            while (true) {
                int c = is.read();

                if (c == -1) {
                    break;
                }

                System.out.println((char) c);
            }

            // 不要在这里关
            // is.close();
        } catch (FileNotFoundException e) {
            System.out.println("3######");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("4######");
            if (is != null) {   // is可能为null，因为文件可能不存在
                try {
                    is.close(); // 关闭资源的时候，也可能出异常
                } catch (IOException e) {
                    //
                }
            }
        }
    }

    // 带资源的try语句
    @Test
    public void testResource() {
        // 把资源在try后面用（）初始化，资源就会自动关闭
        try (InputStream is = new FileInputStream("data\\sentence1.txt");)
        {
            while (true) {
                int c = is.read();

                if (c == -1) {
                    break;
                }

                System.out.println((char) c);
            }

            // 不要在这里关
            // is.close();
        } catch (FileNotFoundException e) {
            System.out.println("3######");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 输出流
    @Test
    public void testOutput() {
        // 第二个参数，表示往文件里面添加内容
        try (OutputStream os = new FileOutputStream("data\\sentence.txt", true)) {
            os.write('A');

            byte[] bytes = new byte[10];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) ('A' + i);
            }

            os.write(bytes);

            // 字符串和字节数组可以相互转化
            // String s = new String(bytes);    => 字节数组转字符串
            // s.getBytes();        => 字符串转字节数组
            os.write("hello world".getBytes());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCopy() {
//        Files.copy("D:\\workspace\\workspace_idea\\codejoys\\data\\sentence.txt",
//                "D:\\b.txt");

        Files.copy("C:\\Windows\\regedit.exe", "D:\\regedit1.exe");
    }

    @Test
    public void testDate() {
        System.out.println(new Date().getTime());

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(new Date().getTime());
    }

    @Test
    public void testCopyBreak() {
        Files.copyBreak("C:\\Windows\\regedit.exe", "D:\\regedit.exe");
    }
}
